<template>
  <el-row :gutter="40" class="panel-group">
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('Temperature','空气温度(°C)')">
        <div class="card-panel-icon-wrapper icon-airtemperature">
          <svg-icon icon-class="airtemperature" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            空气温度(°C)
          </div>
          <count-to :decimals="1" :start-val="0" :end-val="devicedata.tTemperature" :duration="2600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('Humidity','空气湿度(%RH)')">
        <div class="card-panel-icon-wrapper icon-airhumidity">
          <svg-icon icon-class="airhumidity" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            空气湿度(%RH)
          </div>
          <count-to :decimals="1" :start-val="0" :end-val="devicedata.tHumidity" :duration="3000" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('SoilTemperature','土壤温度(°C)')">
        <div class="card-panel-icon-wrapper icon-areatemperature">
          <svg-icon icon-class="areatemperature" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            土壤温度(°C)
          </div>
          <count-to :decimals="1" :start-val="0" :end-val="devicedata.tSoilTemperature" :duration="3200" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('SoilHumidity','土壤湿度(%RH)')">
        <div class="card-panel-icon-wrapper icon-areahumidity">
          <svg-icon icon-class="areahumidity" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            土壤湿度(%RH)
          </div>
          <count-to :decimals="1" :start-val="0" :end-val="devicedata.tSoilHumidity" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('LightIntensity','光照强度(Lux)')">
        <div class="card-panel-icon-wrapper icon-light">
          <svg-icon icon-class="light" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            光照强度(Lux)
          </div>
          <count-to :decimals="1" :start-val="0" :end-val="devicedata.tLightIntensity" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
    <el-col :xs="12" :sm="12" :lg="6" class="card-panel-col">
      <div class="card-panel" @click="handleSetLineChartData('SoilPh','土壤ph值')">
        <div class="card-panel-icon-wrapper icon-phvalue">
          <svg-icon icon-class="phvalue" class-name="card-panel-icon" />
        </div>
        <div class="card-panel-description">
          <div class="card-panel-text">
            土壤ph值
          </div>
          <count-to :decimals="1" :start-val="0" :end-val="devicedata.tSoilPh" :duration="3600" class="card-panel-num" />
        </div>
      </div>
    </el-col>
  </el-row>
</template>

<script>
import CountTo from 'vue-count-to'
import deviceDataApi from "@/api/device"

export default {
  // props: {
  //   deviceData: {
  //     type: Object,
  //     required: true
  //   }
  // },
  components: {
    CountTo
  },
  data() {
    return {
      devicedata: {}
    }
  },
  
  created() {
    this.fetchData();
  },


  watch: {
    $route(to, from) {
      this.fetchData();
    }
    // deviceData: {
    //   deep: true,
    //   handler(val) {
    //     this.devicedata = val;
    //   }
    // }
  },

  methods: {
    handleSetLineChartData(type,name) {
      this.$emit('handleSetLineChartData', type ,name)
    },
    // handleValueName(name){
    //   this.$emit('handleValueName',name)
    // },
    fetchData() {
      deviceDataApi.searchDeviceNewData().then(resp => {
        var result = resp.data;
        this.devicedata = result[0];
        console.log(this.devicedata);
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.panel-group {
  margin-top: 18px;

  .card-panel-col {
    margin-bottom: 32px;
  }

  .card-panel {
    height: 108px;
    cursor: pointer;
    font-size: 12px;
    position: relative;
    overflow: hidden;
    color: #666;
    background: #fff;
    box-shadow: 4px 4px 40px rgba(0, 0, 0, .05);
    border-color: rgba(0, 0, 0, .05);

    &:hover {
      .card-panel-icon-wrapper {
        color: #fff;
      }

      .icon-airtemperature {
        background: #40c9c6;
      }

      .icon-airhumidity {
        background: #36a3f7;
      }

      .icon-areatemperature {
        background: #7d7d16;
      }

      .icon-areahumidity {
        background: #44dbdc
      }

      .icon-light {
        background: #d81e06
      }

      .icon-phvalue {
        background: #589d0a
      }
    }

    .icon-airtemperature {
      color: #40c9c6;
    }

    .icon-airhumidity {
      color: #36a3f7;
    }

    .icon-areatemperature {
      color: #7d7d16;
    }

    .icon-areahumidity {
      color: #44dbdc
    }

    .icon-light {
      color: #d81e06
    }

    .icon-phvalue {
      color: #589d0a
    }

    .card-panel-icon-wrapper {
      float: left;
      margin: 14px 0 0 14px;
      padding: 16px;
      transition: all 0.38s ease-out;
      border-radius: 6px;
    }

    .card-panel-icon {
      float: left;
      font-size: 48px;
    }

    .card-panel-description {
      float: right;
      font-weight: bold;
      margin: 26px;
      margin-left: 0px;

      .card-panel-text {
        line-height: 18px;
        color: rgba(0, 0, 0, 0.45);
        font-size: 16px;
        margin-bottom: 12px;
      }

      .card-panel-num {
        font-size: 20px;
      }
    }
  }
}

@media (max-width:550px) {
  .card-panel-description {
    display: none;
  }

  .card-panel-icon-wrapper {
    float: none !important;
    width: 100%;
    height: 100%;
    margin: 0 !important;

    .svg-icon {
      display: block;
      margin: 14px auto !important;
      float: none !important;
    }
  }
}
</style>
